﻿using HBase; 
using Newtonsoft.Json;
using Newtonsoft.Json.Linq; 
using System;
using System.IO;
using System.Text;

namespace hconnect {
    public abstract  class TestableTask : ZtTask , Synchor {

        Type getType(JObject d,String name) {
            object c = d["className"];
            if (c == null) { 
                consoleOnly(0, "test" + name + "className not fund");
                return null;
            }
            String className = c.ToString();
            Type type = InitServer.getType(className);
            return type;
        }
        virtual public object textParse(object old) {
            String all = readFile();
            all= afterRead(all);
            JObject body = (JObject)JsonConvert.DeserializeObject(all);
            String name = this.GetType().Name;

            JObject d = (JObject)body[name];
            if (d == null) {
                consoleOnly(0, "test 没有节点" + name );
                return null;
            }

            return p1(d, name);
        }
        object p1(JObject d, String name) { 
            Type type = getType(d, name);
            if (type == null) {
                consoleOnly(0, "test 未知的类型:" + className );
                return null;
            }
            String data =d["data"].ToString();
            if (String.IsNullOrEmpty(data)) {
                consoleOnly(0, "test  数据不存在" + name );
                return null;
            }

            return parse(data, type);
        }
        public virtual object parse(String data, Type type) {

            return JsonConvert.DeserializeObject(data, type);
        }



        public virtual String afterRead(String all) {
           // consoleOnly(0, all);
            return all;
        }
       String readFile() { 
                StringBuilder all=new StringBuilder(); 
                StreamReader sr = new StreamReader("Conf/text.json");
               
                String line = sr.ReadLine();
             
                while (line != null) {
                    all.Append(line);
                    line = sr.ReadLine();
                }
                sr.Close();
         
            return all.ToString();
        }
        protected virtual void beforeSync(AbsStatus cs) {

        }
    }
}
